﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Administracion.ar.com.rofex.ws;
using System.Data;
using Generico.Coneccion;

namespace Administracion
{
    public partial class IndicesRofex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                var obj = new ROFEXWebService();
                var ds = obj.Productos();
                lstInfo.DataSource = ds.Tables[0];
                lstInfo.DataTextField = "Nombre";
                lstInfo.DataValueField = "IdProducto";
                lstInfo.DataBind();
            }
        }

        protected void lstInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstInfo.SelectedValue != String.Empty)
            {
                var cn = new ClsConeccion();
                cn.Abrir();
                string sql = "Select * FROM ConfigRofexNews where idProducto = " + lstInfo.SelectedValue;

                var rd = cn.EjecutarSqlDataReader(sql);

                cn.Cerrar();

                var dt = new DataTable();
                dt.Load(rd);
                CheckBoxList1.Items.Clear();
                foreach (DataRow dr in dt.Rows)
                {
                    ListItem lst = new ListItem(dr["Posicion"].ToString());
                    lst.Selected = Convert.ToBoolean(dr["Seleccionado"]);
                    CheckBoxList1.Items.Add(lst);
                }
                /*CheckBoxList1.DataSource = dt;
                CheckBoxList1.DataTextField = "Posicion";
                CheckBoxList1.DataValueField = "Seleccionado";
                CheckBoxList1.DataMember= "Seleccionado";
                CheckBoxList1.DataBind();
                DataTable dt = new DataTable();
                dt.Columns.Add("Vencimiento", DateTime.Today.GetType());
                dt.Columns.Add("Posicion", "".GetType());
                dt.Columns.Add("Precio", 2.3.GetType());
                dt.Columns.Add("AjusteAnterior", 2.3.GetType());
                dt.Columns.Add("Variacion", 2.3.GetType());
                dt.Columns.Add("Volumen", 2.3.GetType());
                dt.Columns.Add("Fecha", DateTime.Today.GetType());

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DataRow[] dr1 = ds.Tables[1].Select("Posicion = '" + dr["Posicion"].ToString() + "'");
                    dt.Rows.Add(
                        dr["Vencimiento"],
                        dr["Posicion"],
                        dr1[0]["Precio"],
                        dr1[0]["AjusteAnterior"],
                        dr1[0]["Variacion"],
                        dr1[0]["Volumen"],
                        dr1[0]["Fecha"]
                        );
                }*/
            }
            
        }

        protected void btnActualizar_Click(object sender, EventArgs e)
        {
            var cn = new ClsConeccion();
            cn.Abrir();

            string sql = "Delete ConfigRofexNews";
            cn.EjecutarSql(sql);
            foreach(ListItem lsi in lstInfo.Items)
            {
                var obj = new ROFEXWebService();
                var ds = obj.MarketData(1, DateTime.Today, DateTime.Today, "", Convert.ToInt32(lsi.Value));
                foreach(DataRow dr in ds.Tables[0].Rows)
                {
                    sql = "Insert Into ConfigRofexNews values (" + lsi.Value + "," + dr["IdPosicion"]+",'" + dr["Posicion"] +"',0)";
                    cn.EjecutarSql(sql);
                }
            }
            cn.Cerrar();
        }

        protected void btnGrabar_Click(object sender, EventArgs e)
        {
            
            var cn = new ClsConeccion();
            cn.Abrir();
             foreach(ListItem lsi in CheckBoxList1.Items)
            {

                string sql = "Update ConfigRofexNews set seleccionado = " + ((lsi.Selected) ? 1 : 0).ToString();
                     sql +=  "where IdProducto = " + lstInfo.SelectedValue + " and Posicion = '"+ lsi.Text +"'";
                cn.EjecutarSql(sql);
                
            }
            cn.Cerrar();
          
        }

    }
}